package com.zangbuge.hellonacos.test;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author lhm
 * @date 2022/8/14
 */
@Slf4j
@RestController
public class PgTestController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private TestMapper testMapper;

    @DS("slave")
    @GetMapping("/getUser")
    public Object getUser() {
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from test");
        log.info(maps.toString());
        return "success";

    }

    @DS("slave")
    @GetMapping("/getUserList")
    public Object getUserList() {
        QueryWrapper<Test> testQueryWrapper = new QueryWrapper<>();
        List<Test> tests = testMapper.selectList(testQueryWrapper);
        log.info(tests.toString());
        return "success";

    }

    @DS("master")
    @GetMapping("/addUser")
    public Object addUser() {
        Test test = new Test();
        test.setId(UUID.randomUUID().toString());
        test.setName("lhm");
        testMapper.insert(test);
        return "success";

    }

}
